﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Phone.Tasks;

namespace TiebaSignWP7
{
    public partial class StartPage : PhoneApplicationPage
    {
        public StartPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string userName = "";
            string password = "";
            using (var uandp = IsolatedStorageFile.GetUserStoreForApplication())
            {
                for (int i = 1; i <= 6; i++)
                {
                    if (uandp.FileExists("nameandpassword" + i + ".txt"))
                    {
                        using (StreamReader srr = new StreamReader(uandp.OpenFile(
                            "nameandpassword" + i + ".txt", FileMode.Open, FileAccess.Read)))
                        {
                            userName = srr.ReadLine();
                            password = srr.ReadLine();
                            switch (i)
                            {
                                case (1):
                                    {
                                        UserName1.Text = userName;
                                        Password1.Password = password;
                                        break;
                                    }
                                case (2):
                                    {
                                        UserName2.Text = userName;
                                        Password2.Password = password;
                                        break;
                                    }
                                case (3):
                                    {
                                        UserName3.Text = userName;
                                        Password3.Password = password;
                                        break;
                                    }
                                case (4):
                                    {
                                        UserName4.Text = userName;
                                        Password4.Password = password;
                                        break;
                                    }
                                case (5):
                                    {
                                        UserName5.Text = userName;
                                        Password5.Password = password;
                                        break;
                                    }
                                case (6):
                                    {
                                        UserName6.Text = userName;
                                        Password6.Password = password;
                                        break;
                                    }
                            }
                        }
                    }
                }
            }
        }

        #region Login Button Tap
        private void Login1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string userName = UserName1.Text;
            string password = Password1.Password;
            SaveInfoAndNavigate(userName, password, 1);
        }

        private void Login2_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string userName = UserName2.Text;
            string password = Password2.Password;
            SaveInfoAndNavigate(userName, password, 2);
        }

        private void Login3_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string userName = UserName3.Text;
            string password = Password3.Password;
            SaveInfoAndNavigate(userName, password, 3);
        }

        private void Login4_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string userName = UserName4.Text;
            string password = Password4.Password;
            SaveInfoAndNavigate(userName, password, 4);
        }

        private void Login5_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string userName = UserName5.Text;
            string password = Password5.Password;
            SaveInfoAndNavigate(userName, password, 5);
        }

        private void Login6_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string userName = UserName6.Text;
            string password = Password6.Password;
            SaveInfoAndNavigate(userName, password, 6);
        }
        #endregion

        private void SaveInfoAndNavigate(string userName, string password, int p)
        {
            //获得独立存储空间
            IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();

            if (myStore.FileExists("nameandpassword" + p + ".txt"))
            {
                myStore.DeleteFile("nameandpassword" + p + ".txt");
            }
            //avoid multi-dispose by try finally segment
            IsolatedStorageFileStream isfs = null;
            try
            {
                isfs = new IsolatedStorageFileStream("nameandpassword" + p + ".txt", FileMode.OpenOrCreate, myStore);
                using (StreamWriter isoFileWriter = new StreamWriter(isfs))
                {
                    isfs = null;
                    //将fileString写入文件流
                    isoFileWriter.WriteLine(userName);
                    isoFileWriter.WriteLine(password);
                }
            }
            finally
            {
                if (isfs != null)
                    isfs.Dispose();
            }
            NavigationService.Navigate(new Uri("/MainPage.xaml?username=" + userName + "&password=" + password, UriKind.RelativeOrAbsolute));
        }

        private void Review_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            MarketplaceReviewTask mrt = new MarketplaceReviewTask();
            mrt.Show();
        }

        private void SomePoint_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.RelativeOrAbsolute));
        }

        #region UserNameTextBox GotFocus
        private void UserName1_GotFocus(object sender, RoutedEventArgs e)
        {
            UserName1.Text = "";
        }

        private void UserName2_GotFocus(object sender, RoutedEventArgs e)
        {
            UserName2.Text = "";
        }

        private void UserName3_GotFocus(object sender, RoutedEventArgs e)
        {
            UserName3.Text = "";
        }

        private void UserName4_GotFocus(object sender, RoutedEventArgs e)
        {
            UserName4.Text = "";
        }

        private void UserName5_GotFocus(object sender, RoutedEventArgs e)
        {
            UserName5.Text = "";
        }

        private void UserName6_GotFocus(object sender, RoutedEventArgs e)
        {
            UserName6.Text = "";
        }
        #endregion
    }
}